\documentclass[paper=a4, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages
\usepackage[UTF8]{ctex}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tikz}
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{clrscode}
\usepackage{sectsty} % Allows customizing section commands
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{float}
\usetikzlibrary{graphs}
\usetikzlibrary{shapes.arrows}
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\usepackage{fancyhdr} % Custom headers and footers
\pagestyle{fancyplain} % Makes all pages in the document conform to the custom headers and footers
\fancyhead{} % No page header - if you want one, create it in the same way as the footers below
\fancyfoot[L]{} % Empty left footer
\fancyfoot[C]{} % Empty center footer
\fancyfoot[R]{\thepage} % Page numbering for right footer
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
\setlength{\headheight}{13.6pt} % Customize the height of the header

\numberwithin{equation}{section} % Number equations within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{figure}{section} % Number figures within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height

\title{
\normalfont \normalsize
\textsc{中国科学院大学}\ \textsc{计算机与控制学院} \\ [25pt] % Your university, school and/or department name(s)
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge 矩阵应用与分析第三次作业 \\ % The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}

\author{黎吉国&201618013229046} % Your name

\date{\normalsize\today} % Today's date or a custom date

\begin{document}

\maketitle % Print the title
\newpage
\section{6th in the exercises of Lec5.pdf}
verify that $rank(A^TA)=rank(A)=rank(AA^T)$ for
\begin{equation*}
  A=\left(
  \begin{array}{rrrr}
    1&3&1&-4\\
    -1&-3&1&0\\
    2&6&2&-8
  \end{array}
  \right)
\end{equation*}
\textbf{Solution:}\\
(1)
\begin{equation*}
A^TA=
\left(
\begin{array}{rrr}
  1&-1&2\\
  3&-3&6\\
  1&1&2\\
  -4&0&-8
\end{array}
\right)
\left(
\begin{array}{rrrr}
  1&3&1&-4\\
  -1&-3&1&0\\
  2&6&2&-8
\end{array}
\right)
=
\left(
\begin{array}{rrrr}
  6&18&4&-20\\
  18&54&12&-60\\
  4&12&6&-20\\
  -20&-60&-20&80
\end{array}
\right)
\end{equation*}
Here we use Guass Elimination to get the rank:\\
\begin{equation*}
  A^TA=\left(
  \begin{array}{rrrr}
    6&18&4&-20\\
    18&54&12&-60\\
    4&12&6&-20\\
    -20&-60&-20&80
  \end{array}
  \right)

  \to
  \left(
  \begin{array}{rrrr}
    4&12&6&-20\\
    18&54&12&-60\\
    6&18&4&-20\\
    -20&-60&-20&-80
  \end{array}
  \right)
  \to
  \left(
  \begin{array}{rrrr}
    4&12&6&-20\\
    0&0&-15&30\\
    0&0&-5&10\\
    0&0&10&-20
  \end{array}
  \right)
  \to
  \left(
  \begin{array}{rrrr}
    4&12&6&-20\\
    0&0&-5&-10\\
    0&0&0&0\\
    0&0&0&0
  \end{array}
  \right)
\end{equation*}\\\\
so $rank(A^TA)=2$.\\
(2)
\begin{equation*}
A=\left(
\begin{array}{rrrr}
  1&3&1&-4\\
  -1&-3&1&0\\
  2&6&2&8
\end{array}
\right)
\end{equation*}\\
use Gauss Elimination to get the rank of A:\\
\begin{equation*}
A=\left(
\begin{array}{rrrr}
  1&3&1&-4\\
  -1&-3&1&0\\
  2&6&2&8
\end{array}
\right)
\to
\left(
\begin{array}{rrrr}
  1&3&1&-4\\
  0&0&2&-4\\
  0&0&0&0
\end{array}
\right)
\end{equation*}\\\\
so $rank(A)=2$.\\
(3)
\begin{equation*}
AA^T=
\left(
\begin{array}{rrrr}
  1&3&1&-4\\
  -1&-3&1&0\\
  2&6&2&-8
\end{array}
\right)
\left(
\begin{array}{rrr}
  1&-1&2\\
  3&-3&6\\
  1&1&2\\
  -4&0&-8
\end{array}
\right)
=
\left(
\begin{array}{rrr}
  27&-9&54\\
  -9&11&-18\\
  54&-18&108
\end{array}
\right)
\end{equation*}
we can get the rank of $AA^T$ with Gauss Elimination:\\
\begin{equation*}
AA^T=
\left(
\begin{array}{rrr}
  27&-9&54\\
  -9&11&-18\\
  54&-18&108
\end{array}
\right)
\to
\left(
\begin{array}{rrr}
  -9&11&-18\\
  27&-9&54\\
  54&-18&108
\end{array}
\right)
\to
\left(
\begin{array}{rrr}
  -9&11&-18\\
  0&24&0\\
  0&0&0
\end{array}
\right)
\end{equation*}\\\\
so $rank(AA^T)=2$.\\
(4)\\
according to (1),(2),(3),we can get that $rank(A^TA)=rank(A)=rank(AA^T)$.
\newpage
\section{9th in the exercises of lec5.pdf}
using least squares techniques, fit the following data:
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
x&-5&-4&-3&-2&-1&0&1&2&3&4&5\\
\hline
y&2&7&9&12&13&14&14&13&10&8&4\\
\hline
\end{tabular}
\end{center}
with a line $y=\alpha_0 + \alpha_1 x$ and then fit the data with a quadratic $y=\alpha_0+\alpha_1 x+ \alpha_2 x^2$. Determine which of these two curves best fits the data by computing the sum of the errors in each case.\\
(1) for $y=\alpha_0+\alpha_1 x$:\\
we set that:
\begin{equation*}
  A=\left(
  \begin{array}{rrrrrrrrrrr}
    1&1&1&1&1&1&1&1&1&1&1\\
    -5&-4&-3&-2&-1&0&1&2&3&4&5
  \end{array}
  \right)^T
\end{equation*}
\begin{equation*}
b=\left(
\begin{array}{rrrrrrrrrrr}
  2&7&9&12&13&14&14&13&10&8&4
\end{array}
\right)^T
\qquad
\alpha=\left(
\begin{array}{rr}
  \alpha_0&\alpha_1
\end{array}
\right)^T
\end{equation*}
then
\begin{equation*}
error(\alpha)=(A\alpha-b)^T(A\alpha-b)
\end{equation*}
we should get the minimum of $error(\alpha)$:
\[\frac{\mathrm{d}error(\alpha)}{\mathrm{d}\alpha}=A^TA\alpha-A^Tb=0 \]
when $rank(A^TA)=2$:
\[\alpha=(ATA)^{-1}A^Tb\]
we can calculate it using Matlab, then we get:
\[\alpha=(9.6364\quad 0.1818)^T\]
in this case: $min(error)=162.9091$.\\
(2) for $y=\alpha_0+\alpha_1 x+\alpha_2 x^2$:\\
we set that:
\begin{equation*}
  A=\left(
  \begin{array}{rrrrrrrrrrr}
    1&1&1&1&1&1&1&1&1&1&1\\
    -5&-4&-3&-2&-1&0&1&2&3&4&5\\
    25&16&9&4&1&0&1&4&9&16&25
  \end{array}
  \right)^T
\end{equation*}
\begin{equation*}
b=\left(
\begin{array}{rrrrrrrrrrr}
  2&7&9&12&13&14&14&13&10&8&4
\end{array}
\right)^T
\qquad
\alpha=\left(
\begin{array}{rr}
  \alpha_0&\alpha_1&\alpha_2
\end{array}
\right)^T
\end{equation*}
then
\begin{equation*}
error(\alpha)=(A\alpha-b)^T(A\alpha-b)
\end{equation*}
we should get the minimum of $error(\alpha)$:
\[\frac{\mathrm{d}error(\alpha)}{\mathrm{d}\alpha}=A^TA\alpha-A^Tb=0 \]
when $rank(A^TA)=3$:
\[\alpha=(ATA)^{-1}A^Tb\]
we can calculate it using Matlab, then we get:
\[\alpha=(13.9720\quad 0.1818 \quad -0.4336)^T\]
in this case: $min(error)=1.6224$.\\
(3) comparation\\
according to the minimum error of two curves, it is obverse that the quadratic fits better the data. Plots both in a coordinate system:\\
\begin{figure}[H]
\centering
\includegraphics[width=6in,height=4in]{least_sqare_quad.jpg}
\caption{least square fitting}
\label{fig:graph}
\end{figure}
\end{document}
